<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>搜索展示</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<link rel="stylesheet" type="text/css" href="../bower_components/mui/dist/css/mui.min.css">
	<link rel="stylesheet" type="text/css" href="../css/base.css">
	<link rel="stylesheet" type="text/css" href="../css/full.css">
	<link rel="stylesheet" type="text/css" href="../css/sousuozhanshi.css">
</head>
<body>
	<div class="page">
	 	<div class="content">
	 		<div class="search">
	 			<div class="search_pic">
	 				<img src="../img/bore/search.png" class="cover">
	 			</div>
			    <input type="text" name="" placeholder="输入经书名称或关键字" class="search_desc">
			    <div class="go">
			    	<button class="cover" id="searchBtn">搜索</button>
			    </div>
			</div>
			<div class="none">
				<p>没有找到相关的经书</p>
			</div>
			<div class="zhanshi" id="zhanshi" data-href="sssss">
				<!-- 展示结果 -->
				<!-- <a href="#" biaoshi="yishoucan">
					<img src="../abs/img/bore/book.png" />
					<span class="fang">《心经》王羲之行书本</span>
					<div class="yishoucang"></div>
					<div class="yichaowan"></div>
				</a>
				<a href="#" biaoshi="yishoucan">
					<img src="../abs/img/bore/book.png" />
					<span class="fang">《心经》王羲之行书本</span>
					<div class="yishoucang"></div>
					<div class="yichaowan"></div>
				</a>
				<a href="#" biaoshi="">
					<img src="../abs/img/bore/book.png" />
					<span class="fang">《心经》王羲之行书本</span>
					<div class="yishoucang"></div>
					<div class="yichaowan"></div> -->
				</a>
			</div>
	 	</div>
	</div>
</body>
<script src="../bower_components/mui/dist/js/mui.min.js"></script>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../js/common.js"></script>
<script>
	var historySearchKey = "historySearch"
	var historySearchList = [];

	// 从localStorage中恢复搜索历史
	function loadHistorySearch() {
		var historySearchString = localStorage.getItem(historySearchKey);
		if (!historySearchString) {
			return ;
		}
		historySearchList = JSON.parse(historySearchString);
	}

	// 将关键词加入到localStorage中(会去除已有的重复词)
	function appendHistorySearch(keyword) {
		var index = historySearchList.indexOf(keyword);
		if (index != -1) {
			historySearchList.splice(index, 1);
		}
		historySearchList.push(keyword);
		localStorage.setItem(historySearchKey, JSON.stringify(historySearchList));
	}

	// 搜索经书
	function searchSutraEdition(keyword) {
		$.ajax({
			url: "/fe/sutra/edition/search",
			method: "GET",
			data: {
				"name": keyword,
				"limit": 9,
				"offset": 0,
			},
			success: function(data) {
				var sutraEditionList = data.data;
				var html = "";
				if (sutraEditionList.length == 0){
					$(".none").show();
					$("#zhanshi").hide();
				}
				else {
					for (var i = 0; i < sutraEditionList.length; i++){
						var sutraEdition = sutraEditionList[i];

						var biaoshi = "none";
						if (sutraEdition.starred) {
							biaoshi = "yishoucan";
						} else if(sutraEdition.finished) {
							biaoshi = "yichaowan";
						}

						html +=
							'<a href="#" biaoshi="' + biaoshi + '" class="sutra_edition_item" ' +
									'data-sutra-id=' + sutraEdition.sutra_id + ' ' +
									'data-sutra-edition-id=' + sutraEdition.sutra_edition_id + '>' +
								'<img src="' + sutraEdition.cover_url + '" />' +
								'<span class="fang">' + sutraEdition.name + '</span>' +
							'</a>';
					}
					$("#zhanshi").html(html);
					$("#zhanshi").show();
					$(".none").hide();

					$(".sutra_edition_item").click(function(e) {
						var sutraID = e.currentTarget.dataset["sutraId"];
						var sutraEditionID = e.currentTarget.dataset["sutraEditionId"];
						window.location.href = "jingshujieshao.html?id=" + sutraID + "&edition_id=" + sutraEditionID;
					});
				}
				refreshTags();
			},
		});
	}

	$("#searchBtn").click(function() {
		var keyword = $(".search_desc").val();
		if (!keyword) {
			return;
		}

		window.location.replace("sousuozhanshi.html?keyword=" + encodeURI(keyword));
		// appendHistorySearch(keyword);
		// searchSutraEdition(keyword);
	})

	function refreshTags() {
		// $("#zhanshi a").each(function(){
		//   	if($(this).attr("biaoshi") == "yishoucan"){
		// 		$(this).children(".yishoucang").css("display","block")
		// 	}else if($(this).attr("biaoshi") == "yichaowan"){
		// 		$(this).children(".yichaowan").css("display","block")
		// 	}else{
		// 		$(this).children(".yichaowan").css("display","none")
		// 		$(this).children(".yishoucang").css("display","none")
		// 	}
		// });
	}

	$(function(){
		loadHistorySearch();
		// 搜索
		var keyword = getParameterByName("keyword");
		searchSutraEdition(keyword);
	})
</script>
</html>